//
//  BaseUIViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/19.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import NSObject_Rx
import HandyJSON
import SnapKit

class BaseUIViewController: UIViewController {
    
    let backButton = UIBarButtonItem()
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        switch navigationBarStyle() {
        case .baseColor, .clearColor:
            return .lightContent
        default:
            return .default
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        let isFirst = self.navigationController?.viewControllers.first == self
        let isTabbar = self.navigationController?.parent is UITabBarController
        
        if (isFirst && isTabbar) || backButtonStyle() == .none {
            return
        }
        
        backButton.image = AppIcon.image("", size: 20)
        backButton.target = self
        backButton.action = #selector(backClick)
        self.navigationItem.leftBarButtonItem = backButton
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarStyle(navigationBarStyle())
    }
    
    @objc func backClick() {
        self.view.endEditing(true)
        if self.navigationController?.viewControllers.first == self {
            self.navigationController?.dismiss(animated: true, completion: nil)
        } else {
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    func navigationBarStyle() -> NavigationBarStyle {
        return .normal
    }
    
    func backButtonStyle() -> BackButtonStyle {
        return .normal
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    deinit {
        print(self.description + " --- deinit")
    }
    
}
